package com.redpack.redpost.util.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.redpack.redpost.util.dao.ArriboAdminDao;
import com.redpack.redpost.util.dao.MultipiezaArriboDao;
import com.redpack.redpost.util.to.ArchivoArribo;
import com.redpack.redpost.util.to.MultipiezaArriboTo;

/**
 * Servlet implementation class GetPaqueteArribo
 */
public class GetPaqueteArribo extends HttpServlet implements javax.servlet.Servlet{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetPaqueteArribo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String respuesta = "";
		String cve_arribo = "";
		String SoloLectura = "";
		

		cve_arribo = request.getParameter("cve_arribo");
		SoloLectura = request.getParameter("SoloLectura");
		
		try 
		{
        
        String Html = ""; 

        if(cve_arribo != "")
  	  	{
        	
        	if(isNumeric(cve_arribo) && isNumeric(SoloLectura))
        	{
        	 List _PaqueteArribo = new ArrayList(); 
	  	     _PaqueteArribo = MultipiezaArriboDao.getPaqueteArribo(Integer.parseInt(cve_arribo));
	

	  		  if(!_PaqueteArribo.isEmpty())
	  		  {
	  			  Html +="<table id='PaquetesArribo' class='table_datosRed'>";
		       	  Html +="<thead>";
		       	  Html +="<tr>";
		       	  Html +="<th>Peso Real</th>";
		       	  Html +="<th>Largo</th>";
		       	  Html +="<th>Ancho</th>";
		       	  Html +="<th>Alto</th>";
		       	  Html +="<th>Peso Volum&eacute;trico</th>";
		       	  
		       	if(Integer.parseInt(SoloLectura) == 0){
		       		Html +="<th></th>";
	        	  }
		       	
		       	  Html +="</tr>";     
		       	  Html +="</thead>";
		       	  Html +="<tbody>";  
	       	
				  java.util.Iterator iter2 = _PaqueteArribo.iterator();
			        while (iter2.hasNext()) {
			        	MultipiezaArriboTo _item = (MultipiezaArriboTo) iter2.next();
	
			        	Html +="<tr class='body_table'>";
			        	Html +="<td>"+_item.getPeso_real_kg()+"</td>";
	 					Html +="<td>"+_item.getLargo_dim_paq_cm()+"</td>";
			        	Html +="<td>"+_item.getAncho_dim_paq_cm()+"</td>";
			        	Html +="<td>"+_item.getAlto_dim_paq_cm()+"</td>";
			        	Html +="<td>"+_item.getPeso_volumetrico_kg()+"</td>";
			        	
			        	if(Integer.parseInt(SoloLectura) == 0){
			        		Html +="<td><img   src='../img/icoError.png'  onclick='EliminarPaquete("+_item.getCve_pieza()+","+_item.getCve_arribo()+",$(this))' /></td>";
			        	  }
			        	
			        	
			        	
			        	Html +="</tr>";
			        	
			        	
			        }
			        
			        Html +="</tbody>";
			        Html +="</table>";
	  		  }

  		  respuesta = Html ;
  		  
  	  	}
  	  }
	 } catch (Exception ex) {
         ex.printStackTrace();
         response.getWriter().write(ex.toString());  
         respuesta = ex.toString();
         //System.out.println(ex);
     }
        response.getWriter().write(respuesta);  
	}
	
	
	private static boolean isNumeric(String cadena){
	    try 
	    {
		    Integer.parseInt(cadena);
		    return true;
	    } 
	    catch (NumberFormatException nfe){
	    	return false;
	    }
	    
	}

}
